<template>
  <div class="navbar-container">
    <!-- 移动端导航栏 -->
    <div
      class="
        webapp-navbar
        container-fluid
        px-3
        d-md-none d-flex
        justify-content-between
        align-items-center
      "
    >
      <!-- logo 图标 -->
      <div class="webapp-logo">
        <img
          @click="$router.push('/community/article')"
          src="../../assets/images/logo2.png"
          class="img-fluid w-100 h-100 rounded"
          alt=""
        />
      </div>
      <!-- 搜索框 -->
      <div class="webapp-search w-100 mx-3">
        <i class="iconfont icon-fangdajing" @click="toSeach"></i>
        <input
          type="input"
          name=""
          v-model="searchContent"
          id=""
          class="w-100 h-100"
          placeholder="知识渔 · 搜索你感兴趣的文章"
          @keyup.enter="toSeach"
        />
      </div>
      <!-- 菜单栏 -->
      <div class="webapp-menu">
        <el-button @click="show3 = !show3">
          <i class="iconfont icon-caidan"></i>
        </el-button>
      </div>
      <!-- 菜单栏伸缩栏 -->
      <div class="webapp-menu-dropdown w-100">
        <el-collapse-transition>
          <div v-show="show3" class="webapp-content">
            <p class="text-start mb-0 mt-2">
              点击感兴趣的标签，跳转到相关文章🐟✨
            </p>
            <div
              class="webapp-menu-box"
              v-for="menu in menu_content"
              :key="menu.id"
            >
              <div
                class="webapp-menu-title"
                v-if="menu.content.length === 0 ? false : true"
              >
                {{ menu.title }}
              </div>
              <div class="webapp-menu-label">
                <div
                  class="webapp-menu-label-box"
                  v-for="item in menu.content"
                  :key="item.id"
                >
                  <div class="webapp-menu-label-box-icon">
                    <i class="iconfont" :class="item.icon"></i>
                  </div>
                  <div class="webapp-menu-label-box-name">{{ item.name }}</div>
                </div>
              </div>
            </div>
          </div>
        </el-collapse-transition>
      </div>
    </div>
    <!-- PC端导航栏 -->
    <div class="pc-navbar d-none d-md-flex justify-content-between">
      <!-- 左侧部分 -->
      <div class="pc-navbar-start">
        <div class="pc-logo">
          <a href="javascript:;">
            <img
              @click="$router.push('/community/article')"
              src="@/assets/images/logo.png"
              class="img-fluid rounded w-100 h-100"
              alt=""
            />
          </a>
        </div>
        <ul class="pc-tags-parent">
          <li v-for="tags in tags_list" :key="tags.id">
            <a href="javascript:;">
              <span>{{ tags.title }}</span>
            </a>
            <div>
              <i class="iconfont" :class="tags.icon"></i>
            </div>
          </li>
        </ul>
      </div>
      <!-- 中间部分 -->
      <div class="pc-navbar-center mx-3">
        <i class="iconfont icon-fangdajing" @click="toSeach"></i>
        <input
          type="input"
          v-model="searchContent"
          @keyup.enter="toSeach"
          name=""
          id=""
          class="w-100"
          placeholder="搜索你感兴趣的文章"
        />
      </div>
      <!-- 右侧部分 -->
      <div class="pc-navbar-end">
        <div class="pc-navbar-end-icon">
          <!-- 存放icon图标的小方块 -->
          <div
            class="pc-navbar-end-icon-box"
            v-for="(item, index) in icon_list"
            :key="index"
            @click="toRouter(item[0])"
          >
            <a href="javascript:;">
              <div class="pc-icon-box-top">
                <i class="iconfont" :class="item[1]"></i>
              </div>
              <div class="pc-icon-box-bottom">{{ item[0] }}</div>
            </a>
          </div>
        </div>
        <div class="pc-navbar-end-omit">
          <a href="javascript:;">
            <i class="iconfont icon-caidan_2"></i>
          </a>
        </div>
        <div class="pc-navbar-end-login">
          <!-- 未登录显示的按钮 -->
          <div class="pc-navbar-off-btn" v-show="!userInfo.userName">
            <a href="javascript:;" @click="$router.replace('/user/login')">
              登录
            </a>
            <div class="pc-navbar-decorate">
              <div></div>
            </div>
            <a href="javascript:;" @click="$router.replace('/user/register')">
              注册
            </a>
          </div>
          <!-- 登录显示的头像 -->
          <div class="pc-navbar-on-box" v-show="userInfo.userName">
            <div class="avatar rounded">
              <img
                v-lazy="userInfo.profile"
                class="img-fluid rounded w-100 h-100"
                alt=""
              />
            </div>
            <!-- 浮入头像显示的操作栏 -->
            <ul class="handlerUser">
              <li class="pc_navbar_userInfo">
                <div class="userInfo_item">
                  <div class="text">
                    {{
                      userInfo.fans
                        ? JSON.parse(userInfo.fans)?.content.length
                        : 0
                    }}
                  </div>
                  <div class="title">粉丝</div>
                </div>
                <div class="userInfo_item">
                  <div class="text">
                    {{
                      userInfo.concern
                        ? JSON.parse(userInfo.concern)?.content.length
                        : 0
                    }}
                  </div>
                  <div class="title">关注</div>
                </div>
                <div class="userInfo_item">
                  <div class="text">
                    {{
                      userInfo.collect
                        ? JSON.parse(userInfo.collect)?.content.length
                        : 0
                    }}
                  </div>
                  <div class="title">收藏</div>
                </div>
              </li>
              <li class="pc_navbar_item">
                <div
                  class="pc_navbar_item_text"
                  v-for="(item, index) in userInfoSelect"
                  :key="index"
                  @click="toPeopleCenter(item.url)"
                >
                  {{ item.title }}
                </div>
              </li>
              <li @click="logout" class="logout">
                <a href="javascript:;">退出登录</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex"; //快速发送

export default {
  name: "NavBar",
  data() {
    return {
      /**
       * PC端属性
       */
      // 左侧导航栏列表
      tags_list: [
        {
          id: 0,
          title: "前端资料",
          icon: "icon-xiala1",
        },
        {
          id: 1,
          title: "后端资料",
          icon: "icon-xiala1",
        },
        {
          id: 2,
          title: "其他标签",
          icon: "icon-xiala1",
        },
        {
          id: 3,
          title: "资源软件",
        },
      ],
      // 右侧icon图标区
      icon_list: [
        ["博客", "icon-wenzhangguanli"],
        ["社区", "icon-shequ"],
        ["笔记", "icon-a-yijianfankuijibiji"],
        ["消息", "icon-xinfeng1"],
        ["收藏", "icon-shoucang"],
        ["联系我", "icon-shouye"],
      ],
      // 移入头像下拉框
      userInfoSelect: [
        {
          title: "个人中心",
          url: "/center",
        },
        // {
        //   title:'个人笔记',
        //   url:''
        // },
        {
          title: "我的消息",
          url: "",
        },
        {
          title: "私信列表",
          url: "",
        },
      ],
      // 搜索内容
      searchContent: "",
      /**
       * 移动端属性
       */
      // 控制菜单打开
      show3: false,
    };
  },
  computed: {
    ...mapState("tags", ["menu_content"]), //tags为组件名
    ...mapState("user", ["userInfo"]),
  },
  methods: {
    ...mapActions("user", {
      updateToken: "updateToken",
      updateUserInfo: "updateUserInfo",
    }),
    // 跳转指定地址
    toRouter(addr) {
      switch (addr) {
        case "社区":
          this.$router.replace("/community/article");
          break;
      }
    },
    // 跳转到个人中心
    toPeopleCenter(url) {
      let newUrl = url.replace("/center", `/center/${this.userInfo.id}`);
      this.$router.push(newUrl);
    },
    // 跳转到搜索页面
    toSeach() {
      let { searchContent } = this;
      this.$router.push(`/search/article?s=${searchContent}`);
      this.searchContent = "";
    },
    // 退出登录
    logout() {
      this.$store.dispatch("user/Logout");
      this.$router.replace("/community");
      this.$win("退出成功");
    },
  },
};
</script>

<style lang="less" scoped>
@media screen and (max-width: 1260px) {
  .navbar-container {
    .pc-navbar {
      // 左侧区域
      .pc-navbar-start {
        .pc-tags-parent {
          li {
            &:nth-of-type(4) {
              display: none;
            }
          }
        }
      }
      // 右侧区域
      .pc-navbar-end {
        .pc-navbar-end-icon {
          .pc-navbar-end-icon-box {
            &:nth-of-type(2) {
              display: none;
            }
            &:nth-of-type(6) {
              display: none;
            }
          }
        }
      }
    }
  }
}
@media screen and (max-width: 1100px) {
  .navbar-container {
    .pc-navbar {
      // 左侧区域
      .pc-navbar-start {
        .pc-tags-parent {
          li {
            &:nth-of-type(3) {
              display: none;
            }
          }
        }
      }
      // 右侧区域
      .pc-navbar-end {
        .pc-navbar-end-icon {
          .pc-navbar-end-icon-box {
            &:nth-of-type(1) {
              display: none;
            }
            &:nth-of-type(3) {
              display: none;
            }
          }
        }
      }
    }
  }
}
.navbar-container {
  position: sticky;
  top: 0;
  z-index: 2004;
  left: 0;
  li {
    list-style-type: none;
  }
  // 移动端导航栏
  .webapp-navbar {
    padding-top: 6px;
    height: 52px;
    padding-bottom: 6px;
    position: relative;
    background: #ffffff;
    box-shadow: 0 4px 12px rgb(0 0 0 / 8%), 0 0 1px rgb(1 0 0 / 10%);
    .webapp-logo {
      width: 28px;
      height: 28px;
    }
    .webapp-search {
      height: 36px;
      position: relative;
      i {
        position: absolute;
        left: 14px;
        top: 4px;
        font-size: 18px;
        font-weight: 700;
      }
      & > input {
        border-radius: 24px;
        padding-left: 38px;
        background-color: #eee;
        font-size: 13px;
        outline: none;
        border: 1px solid transparent;
        transition: 0.3s;
        &:hover {
          border: 1px solid #ccc;
        }
        &:focus {
          border: 1px solid #ccc;
          background: #fff;
        }
      }
    }
    .webapp-menu {
      .el-button {
        border: 0px solid transparent !important;
        padding: 0 !important;
        .icon-caidan {
          color: #737373 !important;
        }
        &:hover {
          background-color: #fff;
        }
        &:focus {
          background-color: #fff;
        }
      }
      i {
        font-size: 28px;
      }
    }
    .webapp-menu-dropdown {
      position: absolute;
      background: #fff;
      box-shadow: 0 4px 12px rgb(0 0 0 / 8%), 0 0 1px rgb(1 0 0 / 10%);
      top: 52px;
      left: 0;
      .webapp-content {
        padding: 10px 18px;
        border-top: 1px solid #dee0e3;
      }
      p {
        margin-bottom: 24px !important;
        font-size: 16px;
      }
      .webapp-menu-box {
        display: flex;
        margin-bottom: 12px;
        &:nth-of-type(2) {
          margin-bottom: 0px !important;
        }
        .webapp-menu-title {
          font-size: 14px;
          margin-right: 14px;
          white-space: nowrap;
        }
        .webapp-menu-label {
          display: flex;
          flex-wrap: wrap;
          .webapp-menu-label-box {
            display: flex;
            align-items: center;
            transition: border 0.4s;
            &:hover {
              border: 1px solid #a9aaaa;
            }
            border: 1px solid #dbdee0;
            border-radius: 6px;
            font-size: 14px;
            padding: 2px 8px;
            text-align: center;
            margin: 0 12px 12px 0;
            .webapp-menu-label-box-icon {
              margin-right: 4px;
              i {
                font-size: 14px;
              }
            }
            .webapp-menu-label-box-name {
              color: #737373;
            }
          }
        }
      }
    }
  }
  // pc端导航栏
  .pc-navbar {
    align-items: center;
    height: 64px;
    position: relative;
    background: #ffffff;
    box-shadow: 0 4px 12px rgb(0 0 0 / 8%), 0 0 1px rgb(1 0 0 / 10%);
    .pc-navbar-start {
      flex: auto;
      max-width: 520px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .pc-logo {
        margin: 0 12px;
        width: 83px;
        height: 35px;
      }
      .pc-tags-parent {
        display: flex;
        padding-left: 0 !important;
        align-items: center;
        flex: 1;
        justify-content: space-evenly;
        li {
          a {
            transition: color 0.4s;
            &:hover {
              color: var(--a-button-hover-color);
              & + div {
                transform: rotate(180deg);
              }
            }
          }
          div {
            transition: transform 0.3s;
            display: inline-block;
          }
          i {
            font-size: 18px;
          }
        }
      }
    }
    .pc-navbar-center {
      flex: auto;
      display: flex;
      align-items: center;
      height: 100%;
      position: relative;
      i {
        position: absolute;
        left: 14px;
        top: 17px;
        font-size: 20px;
        font-weight: 700;
      }
      & > input {
        border-radius: 24px;
        padding-left: 44px;
        background-color: #eee;
        font-size: 14px;
        height: 40px !important;
        outline: none;
        border: 1px solid transparent;
        transition: 0.3s;
        &:hover {
          border: 1px solid #ccc;
        }
        &:focus {
          border: 1px solid #ccc;
          background: #fff;
        }
      }
    }
    .pc-navbar-end {
      flex: auto;
      max-width: 500px;
      display: flex;
      align-items: center;
      .pc-navbar-end-icon {
        flex: auto;
        align-items: center;
        justify-content: space-evenly;
        display: flex;
        align-content: space-between;
        align-items: center;
        & .pc-navbar-end-icon-box:nth-of-type(2) {
          i {
            font-size: 23px !important;
          }
        }
        & .pc-navbar-end-icon-box:nth-of-type(3) i {
          font-size: 22px !important;
        }
        & .pc-navbar-end-icon-box:nth-of-type(4) i {
          font-size: 26px !important;
        }
        & .pc-navbar-end-icon-box:nth-of-type(5) i {
          font-size: 24px !important;
        }
        & .pc-navbar-end-icon-box:nth-of-type(6) i {
          font-size: 22px !important;
        }
        .pc-navbar-end-icon-box {
          // 出现浮入效果
          a {
            display: flex;
            flex-direction: column;
            align-items: center;
            transition: color 0.4s;
            &:hover {
              color: var(--a-button-hover-color);
            }
          }
          .pc-icon-box-top {
            height: 26px;
            width: 30px;
            position: relative;
            i {
              /* 水平垂直居中 */
              position: absolute;
              top: 50%;
              left: 50%;
              transform: translate(-50%, -50%);
              font-size: 20px;
              font-weight: 600;
            }
          }
        }
      }
      .pc-navbar-end-omit {
        margin: 0 18px 0 4px;
      }
      .pc-navbar-end-login {
        min-width: 70px;
        height: 64px;
        padding-right: 14px;
        cursor: url("@/assets/mouse/x2.cur"), auto !important;
        &:hover {
          .pc-navbar-on-box {
            .handlerUser {
              display: block;
            }
          }
        }
        .pc-navbar-off-btn {
          /* 水平垂直居中 */
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 108px;
          height: 32px;
          background: inherit;
          position: relative;
          background-color: rgba(255, 255, 255, 1);
          box-sizing: border-box;
          border-width: 1px;
          border-style: solid;
          border-color: rgba(209, 209, 209, 1);
          border-radius: 4px;
          display: flex;
          justify-content: space-evenly;
          align-items: center;
          transition: border 0.4s;
          &:hover {
            border-color: #767676;
            a {
              color: #111;
              border-color: #767676;
            }
            .pc-navbar-decorate {
              border: 1px solid #111;
              & > div {
                background: #111;
              }
            }
          }
          a {
            transition: color 0.4s;
            color: #737373;
          }
          .pc-navbar-decorate {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            position: relative;
            transition: border 0.4s;
            border: 1px solid #737373;
            & > div {
              border-radius: 50%;
              /* 水平垂直居中 */
              position: absolute;
              top: 50%;
              left: 50%;
              transition: background 0.4s;
              transform: translate(-50%, -50%);
              width: 4px;
              height: 4px;
              background: #737373;
            }
          }
        }
        .pc-navbar-on-box {
          height: 100%;
          position: relative;
          .avatar {
            width: 36px;
            height: 36px;
            /* 水平垂直居中 */
            position: absolute;
            top: 50%;
            left: 50%;
            background: #f6f6f6;
            transform: translate(-50%, -50%);
          }
          .handlerUser {
            display: none;
            position: absolute;
            top: 64px;
            right: 0;
            background: #fff;
            border-radius: 8px;
            box-shadow: var(--main-box--shadow);
            padding: 20px 0 20px 0;
            width: 200px;
            li {
              // color: var(--main--text--color);
              text-align: center;
            }
            .pc_navbar_userInfo {
              display: flex;
              justify-content: space-between;
              padding: 0 20px;
              .userInfo_item {
                height: 60px;
              }
            }
            .pc_navbar_item {
              .pc_navbar_item_text {
                margin-bottom: 10px;
              }
            }
            .logout {
              margin-top: 20px;
            }
          }
        }
      }
    }
  }
}
</style>